<?php

namespace addons\shopro\controller\user;

use addons\shopro\controller\commission\Goods;
use addons\shopro\controller\Common;
use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\user\WalletLog as UserWalletLogModel;

class WalletLog extends Common
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $type = $this->request->param('type', 'money');
        $tab = $this->request->param('tab', 'all');
        $list_rows = $this->request->param('list_rows', 10);
        $date = $this->request->param('date/a');
        $user = auth_user();
        $where['user_id'] = $user->id;
        switch ($tab) {
            case 'income':
                $where['amount'] = ['>', 0];
                break;
            case 'expense':
                $where['amount'] = ['<', 0];
                break;
        }
        if(!in_array($type, [UserWalletLogModel::SYS_FUND_TYPE, UserWalletLogModel::SYS_GREEN_SCORE_TYPE]))
        {
            $income = UserWalletLogModel::where('user_id', $user->id)
                ->{$type}()
                ->where('amount', '>', 0)
                ->whereTime('createtime', 'between', $date)
                ->sum('amount');
            $expense = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '<', 0)->whereTime('createtime', 'between', $date)->sum('amount');

            $logs = UserWalletLogModel::where($where)
                ->{$type}()
                ->whereTime('createtime', 'between', $date)
                ->order('createtime', 'desc')
                ->paginate($list_rows);
            foreach ($logs as &$item){
                if($item['event']=='order_give1'){
                    if($item['ext']['order_id']){

                        //    $good=Order::where('id',$v['ext']['order_id'])->find();
                        $item['good_image']=Db('shopro_order_item')->where('order_id',$item['ext']['order_id'])->value('goods_image');
                    }
                }else{
                    $item['good_image']='';
                }


            }
            $this->success('获取成功', ['list' => $logs, 'income' => $income, 'expense' => $expense]);
        }else
        {

            $income = UserWalletLogModel::where('amount', '>', 0)->{$type}()->whereTime('createtime', 'between', $date)->sum('amount');
            $expense = UserWalletLogModel::where('amount', '<', 0)->{$type}()->whereTime('createtime', 'between', $date)->sum('amount');
            unset($where['user_id']);
            $logs = UserWalletLogModel::where($where)->{$type}()->whereTime('createtime', 'between', $date)->order('createtime', 'desc')->paginate($list_rows);
            foreach ($logs as &$item){
                if($item['event']=='order_give1'){
                    if($item['ext']['order_id']){

                        //    $good=Order::where('id',$v['ext']['order_id'])->find();
                        $item['good_image']=Db('shopro_order_item')->where('order_id',$item['ext']['order_id'])->value('goods_image');
                    }
                }else{
                    $item['good_image']='';
                }

            }
            $this->success('获取成功', ['list' => $logs, 'income' => $income, 'expense' => $expense]);
        }

    }
    public function alllog()
    {
        $type = $this->request->param('type', 'money');
        $tab = $this->request->param('tab', 'all');
        $user = auth_user();
        $where['user_id'] = $user->id;

        switch ($tab) {
            case 'income':
                $where['amount'] = ['>', 0];
                break;
            case 'expense':
                $where['amount'] = ['<', 0];
                break;
        }
        if(!in_array($type, [UserWalletLogModel::SYS_FUND_TYPE, UserWalletLogModel::SYS_GREEN_SCORE_TYPE]))
        {
            $income = UserWalletLogModel::where('user_id', $user->id)->where('createtime','>',1697558400)->{$type}()->where('amount', '>', 0)->sum('amount');
            $expense = UserWalletLogModel::where('user_id', $user->id)->where('createtime','>',1697558400)->{$type}()->where('amount', '<', 0)->sum('amount');
            unset($where['user_id']);
            $logs = UserWalletLogModel::where($where)->where('createtime','>',1697558400)->{$type}()->order('createtime', 'asc')->select();
            $this->success('获取成功', ['list' => $logs, 'income' => $income, 'expense' => $expense]);
        }else
        {
            $income = UserWalletLogModel::where('amount', '>', 0)->where('createtime','>',1697558400)->{$type}()->sum('amount');
            $expense = UserWalletLogModel::where('amount', '<', 0)->where('createtime','>',1697558400)->{$type}()->sum('amount');
            unset($where['user_id']);
            $logs = UserWalletLogModel::where($where)->where('createtime','>',1697558400)->{$type}()->order('createtime', 'asc')->select();
            $this->success('获取成功', ['list' => $logs, 'income' => $income, 'expense' => $expense]);
        }

    }
    public function cashlog()
    {
        $type = $this->request->param('type', 'money');
        $type='commission';
        $tab = $this->request->param('tab', 'all');
        $list_rows = $this->request->param('list_rows', 10);
        $date = $this->request->param('date/a');
        $user = auth_user();
        $where['user_id'] = $user->id;
        switch ($tab) {
            case 'income':
                $where['amount'] = ['>', 0];
                break;
            case 'expense':
                $where['amount'] = ['<', 0];
                break;
        }
        if(!in_array($type, [UserWalletLogModel::SYS_FUND_TYPE, UserWalletLogModel::SYS_GREEN_SCORE_TYPE]))
        {
            $income = UserWalletLogModel::where('user_id', $user->id)
                ->{$type}()->where('amount', '>', 0)
                ->whereTime('createtime', 'between', $date)
                ->sum('amount');
            $expense = UserWalletLogModel::where('user_id', $user->id)
                ->{$type}()->where('amount', '<', 0)
                ->whereTime('createtime', 'between', $date)
                ->sum('amount');
            $logs = UserWalletLogModel::where($where)->{$type}()
                ->whereTime('createtime', 'between', $date)
                ->where('event','=','churujin')
                ->order('createtime', 'desc')
                ->paginate($list_rows);
            $this->success('获取成功', ['list' => $logs, 'income' => $income, 'expense' => $expense]);
        }else
        {
            $income = UserWalletLogModel::where('amount', '>', 0)->{$type}()->whereTime('createtime', 'between', $date)->sum('amount');
            $expense = UserWalletLogModel::where('amount', '<', 0)->{$type}()->whereTime('createtime', 'between', $date)->sum('amount');
           // unset($where['user_id']);
            $logs = UserWalletLogModel::where($where)->{$type}()
                ->where('event','=','churujin')
                ->whereTime('createtime', 'between', $date)
                ->order('createtime', 'desc')
                ->paginate($list_rows);
            $this->success('获取成功', ['list' => $logs, 'income' => $income, 'expense' => $expense]);
        }

    }
}
